package com.abigdreamer.im.server.ui;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JOptionPane;

import com.abigdreamer.im.server.module.EIMServer;
import com.abigdreamer.im.server.sound.SoundPlay;
import com.abigdreamer.im.server.ui.desktop.EIMDesktop;
import com.abigdreamer.im.server.ui.resourse.ResourseMannager;

/**
 */
public class ServerMainFrame extends BaseFrame implements Runnable{
    
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static ServerMainFrame instance;
	private EIMServer  server;
	public static ServerMainFrame getInstance(){
		if(instance==null){
			instance=new ServerMainFrame();
		}
		return instance;
	}
    private ServerMainFrame() {
    	super();
        initComponents();
    }
    
    private void initComponents() {
        leftPanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        propetryTree = PropetryTree.getInstance(EIMDesktop.getInstance().getDeskTopConfig());
        chartPanel = new MemoryMonitor();
        cupPanel = new CPUMonitor();
        shangPanel = new javax.swing.JPanel();
        jToolBar1 = new javax.swing.JToolBar();
        jToolBar1.setEnabled(false);
        startButton = new javax.swing.JButton();
        refreshButton = new javax.swing.JButton();
        stopButton = new javax.swing.JButton();
        stopButton.setEnabled(false);
        refreshButton.setEnabled(false);
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        desktopPane = EIMDesktop.getInstance();
        cmdScrollPane = new javax.swing.JScrollPane();
        CMDPane = new CMDTextPanel();
        menuBar = new javax.swing.JMenuBar();
        sysMenu = new javax.swing.JMenu();
        aboutMenuItem = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JSeparator();
        exitMenuItem = new javax.swing.JMenuItem();
        jScrollPane1.setViewportView(propetryTree);

        javax.swing.GroupLayout leftPanelLayout = new javax.swing.GroupLayout(leftPanel);
        leftPanel.setLayout(leftPanelLayout);
        leftPanelLayout.setHorizontalGroup(
            leftPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(chartPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 130, Short.MAX_VALUE)
            .addComponent(cupPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        leftPanelLayout.setVerticalGroup(
            leftPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, leftPanelLayout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 115, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cupPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(chartPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        startButton.setIcon(ResourseMannager.getIconUrlByName("start.png"));
        startButton.setText("\u542f\u52a8\u670d\u52a1");
        startButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                startButtonMouseClicked(evt);
            }
        });

        jToolBar1.add(startButton);

        refreshButton.setIcon(ResourseMannager.getIconUrlByName("refresh.png"));
        refreshButton.setText("\u5237\u65b0");
        refreshButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                refreshButtonMouseClicked(evt);
            }
        });

        jToolBar1.add(refreshButton);

        stopButton.setIcon(ResourseMannager.getIconUrlByName("stop.png"));
        stopButton.setText("\u505c\u6b62\u670d\u52a1");
        stopButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                stopButtonMouseClicked(evt);
            }
        });

        jToolBar1.add(stopButton);

        jLabel1.setText("\u5728\u7ebf\u4eba\u6570:");
        jToolBar1.add(jLabel1);

        jLabel2.setText("101");
        jToolBar1.add(jLabel2);

        javax.swing.GroupLayout shangPanelLayout = new javax.swing.GroupLayout(shangPanel);
        shangPanel.setLayout(shangPanelLayout);
        shangPanelLayout.setHorizontalGroup(
            shangPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 688, Short.MAX_VALUE)
        );
        shangPanelLayout.setVerticalGroup(
            shangPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        cmdScrollPane.setViewportView(CMDPane);

        sysMenu.setText("\u7cfb\u7edf");
        aboutMenuItem.setText("\u5173\u4e8e\u7cfb\u7edf");
        sysMenu.add(aboutMenuItem);

        sysMenu.add(jSeparator1);

        exitMenuItem.setText("\u9000\u51fa\u7cfb\u7edf");
        exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitMenuItemActionPerformed(evt);
            }
        });

        sysMenu.add(exitMenuItem);

        menuBar.add(sysMenu);

        setJMenuBar(menuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(leftPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cmdScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 552, Short.MAX_VALUE)
                    .addComponent(desktopPane, javax.swing.GroupLayout.DEFAULT_SIZE, 552, Short.MAX_VALUE)))
            .addComponent(shangPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(shangPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(desktopPane, javax.swing.GroupLayout.DEFAULT_SIZE, 232, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cmdScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(leftPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );
        addWindowListener(new WindowAdapter(){
	    	public void windowClosing(WindowEvent e){
	    	   	setVisible(false);
	    	}});
        this.setSize(800, 700);
    }

	public void run() {
		ServerMainFrame serverMainFrame=ServerMainFrame.getInstance();
		BaseFrame.centerWindow(serverMainFrame);
		serverMainFrame.setVisible(true);
	}

    private void stopButtonMouseClicked(java.awt.event.MouseEvent evt) {
     	int sure=getOptionDialog("关闭服务器?");
    	if(sure==0){
    		server=EIMServer.getInstance();
        	boolean re=server.closeServer();
        	if(re){
        		stopButton.setEnabled(false);
        		refreshButton.setEnabled(false);
        		startButton.setEnabled(true);
        		EIMTrayIcon.getInstanse().changeIconIamge("closed");
        		ToolTip.startBubbleTip("企业即时通讯服务器已经关闭了");
        		
        	}else{
        		javax.swing.JOptionPane.showMessageDialog(this,"服务器已经关闭,不能关闭两次");
        	}
    	}
    }

    private void refreshButtonMouseClicked(java.awt.event.MouseEvent evt) {
     	int sure=getOptionDialog("刷新服务器?");
    	if(sure==0){
    		server=EIMServer.getInstance();
        	boolean re=server.refreshServer();
        	if(re){
        		EIMTrayIcon.getInstanse().changeIconIamge("refresh");
        		ToolTip.startBubbleTip("企业即时通讯服务器成功刷新");
        	}else{
        		javax.swing.JOptionPane.showMessageDialog(this,"服务器没启动，不能刷新");
        	}
    	}
    }

    private void startButtonMouseClicked(java.awt.event.MouseEvent evt) {
    	int sure=getOptionDialog("开启服务器");
    	if(sure==0){
    	   	server=EIMServer.getInstance();
        	boolean re=server.startServer();
        	if(re){
                stopButton.setEnabled(true);
                startButton.setEnabled(false);
                refreshButton.setEnabled(true);
                EIMTrayIcon.getInstanse().changeIconIamge("start");
                ToolTip.startBubbleTip("企业即时通讯服务器成功开启");
        	}else{
        		javax.swing.JOptionPane.showMessageDialog(this,"服务器已经启动,不能启动两次");
        	}
    	}
 
    }

    private int getOptionDialog(String msg){
    	Object[] options = {"确定?","取消?"};
    	int response=JOptionPane.showOptionDialog ( this, "您确定"+msg,msg,JOptionPane.YES_OPTION ,JOptionPane.PLAIN_MESSAGE,
                null, options, options[0] );
    	return response;
    }
    
    private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
    	int sure=getOptionDialog("退出并关闭服务器？");
    	if(sure==0){
    		SoundPlay.playClosedSound();
    		System.exit(3);
    	}
    }
    
    public void addCMDInfo(String info){
    	if(!CMDPane.isCmdState()){
    		String cmd=CMDPane.getText();
        	cmd=cmd+info;
        	CMDPane.setText(cmd);
        	CMDPane.setCaretPosition(CMDPane.getDocument().getLength());
    	}
    }
    

    // 变量声明 - 不进行修改//GEN-BEGIN:variables
    private CMDTextPanel CMDPane;
    private javax.swing.JMenuItem aboutMenuItem;
    private MemoryMonitor chartPanel;
    private CPUMonitor cupPanel;
    private javax.swing.JDesktopPane desktopPane;
    private javax.swing.JMenuItem exitMenuItem;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane cmdScrollPane;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JPanel leftPanel;
    private javax.swing.JMenuBar menuBar;
    private PropetryTree propetryTree;
    private javax.swing.JButton refreshButton;
    private javax.swing.JPanel shangPanel;
    private javax.swing.JButton startButton;
    private javax.swing.JButton stopButton;
    private javax.swing.JMenu sysMenu;
    // 变量声明结束//GEN-END:variables
    
}
